//
//  DataManager.m
//  HandheldHospital
//
//  Created by zhangchao on 16/2/26.
//  Copyright © 2016年 zhangchao. All rights reserved.
//

#import "DataManager.h"
#import "ZC_AsynchronousBlock.h"

@implementation DataManager

+ (instancetype)defaultManager
{
    static DataManager *manager = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [[DataManager alloc] init];
        manager.myUser = [AVUser currentUser];
        manager.userType = [AVUser currentUser][@"userType"];
        if(manager.myUser != nil)
        {
            if([manager.userType isEqualToString:@"医生"])
            {
                manager.dataArray = [NSMutableArray array];
            }
            else
            {
                AVQuery *query = [AVQuery queryWithClassName:@"PatientData"];
                [query whereKey:@"currentUser" equalTo:manager.myUser];
                [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
                    manager.dataArray = [NSMutableArray arrayWithArray:objects];
                }];
            }
            //获取Token
            [manager getToken];
            //获取医生信息
            [manager getSeekModel];
        }
        //获取所有医生
        [manager getSeekArray];
        //获取所有医院
        [manager getHospitalArray];
    });
    return manager;
}


//获取Token
- (void)getToken
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:[AVUser currentUser].username forKey:@"name"];
    [dic setValue:[AVUser currentUser].email forKey:@"userId"];
    //取得当前用户的头像信息
    AVFile *file = [AVUser currentUser][@"Picture"];
    [dic setValue:file.url forKey:@"portraitUri"];
    //获取body
    NSData *da = [ZC_AsynchronousBlock httpBodyFromParamDictionary:dic];
    
    [ZC_AsynchronousBlock asynchronousPOSTWithURLString:@"https://api.cn.ronghub.com/user/getToken.json" AppKey:@"uwd1c0sxdbad1" AppSecret:@"1ery3DRvC5k" body:da PassBlock:^(NSData *data) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
        //取得当前token
        self.token = dict[@"token"];
    }];
}

//获取所有医生
- (void)getSeekArray
{
    AVQuery *query = [AVQuery queryWithClassName:@"SeekModel"];
    [query includeKey:@"currentUser"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.seekArray = [NSArray arrayWithArray:objects];
    }];

}

//获取所有医院
- (void)getHospitalArray
{
    AVQuery *query = [AVQuery queryWithClassName:@"Hospital"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.hopitalArray = [NSArray arrayWithArray:objects];
    }];
}

//获取所有医院并刷新表视图
- (void)getHospitalArrayAndRefreshTableView:(UITableView *)tableView
{
    [MBProgressHUD showHUDAddedTo:tableView animated:YES];
    AVQuery *query = [AVQuery queryWithClassName:@"Hospital"];
    [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        self.hopitalArray = [NSArray arrayWithArray:objects];
        [tableView reloadData];
        [MBProgressHUD hideAllHUDsForView:tableView animated:YES];
    }];
}


//获取医生信息
- (void)getSeekModel
{
    if([self.userType isEqualToString:@"医生"])
    {
        AVQuery *query = [AVQuery queryWithClassName:@"SeekModel"];
        [query whereKey:@"currentUser" equalTo:[AVUser currentUser]];
        [query includeKey:@"currentUser"];
        [query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            self.seek = [objects firstObject];
        }];
    }
}

@end
